﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Charts;
using DAL.Models;
using DAL.BAL;

namespace WpfClient.Windows
{
    /// <summary>
    /// Interaction logic for DashBoard.xaml
    /// </summary>
    public partial class DashBoard : Window
    {
        private DateTime DateFrom = DateTime.MinValue;

        private DateTime DateTo = DateTime.MinValue;

        public DashBoard()
        {
            InitializeComponent();

            DateFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

            DateTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);

            string additionContent = string.Format(" ({0} - {1})", DateFrom.ToShortDateString(), DateTo.ToShortDateString());

            chart1.Titles[0].Content = chart1.Titles[0].Content + additionContent;

            chart2.Titles[0].Content = chart2.Titles[0].Content + additionContent;
            
            chart3.Titles[0].Content = chart3.Titles[0].Content + additionContent;

            Refresh();
        }

        private void RefreshChart(ChartControl chart)
        {
            chart.BeginInit();

            (chart.Diagram as XYDiagram2D).AxisY.CustomLabels.Clear();

            List<ReportPOCO> reportValues = null;

            if (chart.Name == chart1.Name)
                reportValues = Business.GetCandidatesReportPerDay(DateFrom, DateTo, 4);
            else
                if (chart.Name == chart2.Name)
                    reportValues = Business.GetVacanciesReportPerDay(DateFrom, DateTo);
                else
                    if (chart.Name == chart3.Name)
                        reportValues = Business.GetSendResumeReportPerDay(DateFrom, DateTo, 0);

            (chart.Diagram as XYDiagram2D).Series[0].DataSource = reportValues;

            (chart.Diagram as XYDiagram2D).Series[0].ArgumentDataMember = "Date";

            (chart.Diagram as XYDiagram2D).Series[0].ValueDataMember = "Value";

            foreach (var rp in reportValues.OrderBy(c => c.Value).GroupBy(c => c.Value).Select(c => new
            {
                Value = c.Key
            }))
            {
                (chart.Diagram as XYDiagram2D).AxisY.CustomLabels.Add(new CustomAxisLabel(rp.Value, rp.Value.ToString()));
            }

            chart.EndInit();

            chart.Animate();
        }

        public void Refresh()
        {
            RefreshChart(chart1);

            RefreshChart(chart2);
            
            RefreshChart(chart3);
        }

        private void bRefresh_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            Refresh();
        }
    }
}
